<?php
namespace backend\controllers;

use backend\models\AuthRule;
use backend\models\Information;
use common\components\BaseController;
use Yii;
use \common\helps\tools;
use \common\helps\code;
use yii\db\Query;


/**
 * Information controller
 */
class InformationController extends BaseController
{

    public $modelClass = 'backend\models\Information';
    public function init()
    {
//        $this->enableAuth=false;
        parent::init();
    }


    /**
     * 获取资讯列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetInformationList(){
        $request =$this->request;
        $model = new Information();
        $params = $request->queryParams;
        $total_data = $model->getInformationList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','标题','分类','发布人/ID','是否推荐','状态','创建时间'];
            foreach($total_data as &$value){
                if($value['status']>1){
                    $value['status'] ='停用';
                }else{
                    $value['status'] ='启用';
                }
                if($value['is_recommend']>1){
                    $value['is_recommend'] ='否';
                }else{
                    $value['is_recommend'] ='是';
                }

            }
            tools::ToPHPEXECL($tableHeader,$total_data,'资讯列表');
        }


        return code::success($data);

    }

    /**
     * 删除节点
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelInformation(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new Information();
            $params = $request->bodyParams;
            if(empty($params['id'])){
                return code::error('','参数错误');
            }
            $res = $model->delInformation(intval($params['id']));
            if($res){
                return code::success();
            }else{
                return code::error('error');
            }

        }
    }

    /**
     * 编辑资讯
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditInformation(){
        if($this->request->isPost) {
            $admin_model = new Information();
            $params = $this->request->bodyParams;
            $params['user_id'] = $this->user_id;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $admin_model->scenario='default_update';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editInformation($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }


    /**
     * 新增资讯
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddInformation(){
        if($this->request->isPost) {
            $admin_model = new Information();
            $params = $this->request->bodyParams;
            $params['user_id'] = $this->user_id;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $admin_model->scenario='default_create';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editInformation($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 获取资讯详情
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetInformationInfo(){
        $request =$this->request;
        $params = $request->queryParams;
        if(empty($params['id'])){  return code::error('','参数错误');}
        $data = (new Query())
            ->select('information.*,information_class.title as class_title')
            ->from('information')
            ->leftJoin('information_class','information_class.id=information.mid')
            ->where(['information.id'=>intval(trim($params['id']))])
            ->one();
        if(!empty($data)){
            return code::success($data);
        }else{
            return code::error('','未查询到相关数据');
        }

    }

}
